package com.cjh.wxdemoserver.peopel.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjh.wxdemoserver.base.exception.BizException;
import com.cjh.wxdemoserver.peopel.dto.PeopelQueryDTO;
import com.cjh.wxdemoserver.peopel.dto.PeopleSexChartDataDTO;
import com.cjh.wxdemoserver.peopel.entity.Peopel;
import com.cjh.wxdemoserver.peopel.enums.PeopelResultCode;
import com.cjh.wxdemoserver.peopel.mapper.PeopelMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ChenJH
 * @description Peopel业务层
 */

@Service
public class PeopelServiceImpl implements PeopelService {
    @Resource
    private PeopelMapper peopelMapper;

    @Override
    public Page<Peopel> page(Page<Peopel> page, PeopelQueryDTO dto) {
        LambdaQueryWrapper<Peopel> wrapper = new LambdaQueryWrapper<>();
        wrapper.likeRight(StringUtils.isNotEmpty(dto.getName()), Peopel::getName, dto.getName());
        wrapper.ge(ObjectUtils.isNotEmpty(dto.getStartBirthday()), Peopel::getBirthday, dto.getStartBirthday());
        wrapper.le(ObjectUtils.isNotEmpty(dto.getEndBirthday()), Peopel::getBirthday, dto.getEndBirthday());
        wrapper.orderByDesc(Peopel::getCreateTime);
        return peopelMapper.selectPage(page, wrapper);
    }

    @Override
    public Peopel getById(Long id) {
        if (ObjectUtils.isEmpty(id)) {
            throw new BizException(PeopelResultCode.ID_NOT_EMPTY);
        }
        return peopelMapper.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(Peopel peopel) {
        check(peopel);
        peopelMapper.insert(peopel);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Peopel peopel) {
        check(peopel);
        Peopel byId = getById(peopel.getId());
        if (ObjectUtils.isEmpty(byId)) {
            throw new BizException(PeopelResultCode.DATA_NOT_EMPTY);
        }
        peopelMapper.updateById(peopel);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        int i = peopelMapper.deleteById(id);
        if(i == 0) {
            throw new BizException(PeopelResultCode.DELETE_FAIL);
        }
    }

    @Override
    public List<PeopleSexChartDataDTO> getSexChartData() {
        return peopelMapper.getSexChartData();
    }

    private void check(Peopel peopel) {
        if(StringUtils.isEmpty(peopel.getName())) {
            throw new BizException(PeopelResultCode.NAME_NOT_EMPTY);
        }
        if(StringUtils.isEmpty(peopel.getSex())) {
            throw new BizException(PeopelResultCode.SEX_NOT_EMPTY);
        }
        if(ObjectUtils.isEmpty(peopel.getBirthday())) {
            throw new BizException(PeopelResultCode.BIRTHDAY_NOT_EMPTY);
        }
    }
}
